$(function () {
    $("#jqGrid").jqGrid({
        url: '../sbydgl/list',
        datatype: "json",
        colModel: [			
			{ label: 'ydId', name: 'ydId', index: 'yd_id', width: 50, key: true ,hidden:true},
			{ label: '商保类型', name: 'sblx', index: 'sblx', width: 80 ,formatter: getDictInfo},
			{ label: '保险公司', name: 'bxgs', index: 'bxgs', width: 80 ,formatter: getDictInfo},
			{ label: '保险金额', name: 'bxje', index: 'bxje', width: 80 }, 			
			{ label: '商保基数id', name: 'sbjsglId', index: 'sbjsgl_id', width: 80,hidden:true },
			{ label: '保险编号', name: 'bxCode', index: 'bx_code', width: 80},
			{ label: '生效日期', name: 'startDate', index: 'start_date', width: 80 }, 			
			{ label: '失效日期', name: 'endDate', index: 'end_date', width: 80 }, 			
			{ label: '备注', name: 'bz', index: 'bz', width: 500 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.getDet();//主表明细
    vm.getSelectDet();//需要替换的明细
    var r = utils.getDictList("sblx,sex,bxgs");
    vm.sblx=r.sblx;
    vm.sex=r.sex;
    vm.bxgs=r.bxgs;
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		sbydgl: {},
        sblx: {},
        sex: {},
        bxgs: {},
        sbydglDet:{},
        batchcodelist:{},
        q:{
            bxgs:null,
            sblx:null,
            bxje:null
        },
        qdet:{
            username:null,
            startDate:null,
            endDate:null,
            enabled:null
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.sbydgl = {};
		},
		update: function (event) {
			var ydId = getSelectedRow();
			if(ydId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(ydId)
            //this.queryDet();//加载明细
		},
		saveOrUpdate: function (event) {
			var url = vm.sbydgl.ydId == null ? "../sbydgl/save" : "../sbydgl/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.sbydgl),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ydIds = getSelectedRows();
			if(ydIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../sbydgl/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ydIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(ydId){
			$.get("../sbydgl/info/"+ydId, function(r){
                vm.sbydgl = r.sbydgl;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData : {
                    bxgs: vm.q.bxgs,
                    sblx: vm.q.sblx,
                    bxje: vm.q.bxje
                },
                page:page
            }).trigger("reloadGrid");
		},
		getDet:function(){
            $("#jqGridDet").jqGrid({
                url: '../sbydgldet/list',
                datatype: "local",
                colModel: [
                    { label: 'yddetId', name: 'yddetId', index: 'yddet_id', width: 50, key: true ,hidden:true},
                    { label: '商保异动id', name: 'ydId', index: 'yd_id', width: 80 ,hidden:true},
                    { label: '用户id', name: 'userId', index: 'user_id', width: 80 ,hidden:true},
                    { label: '身份证', name: 'idNo', index: 'id_no', width: 80 },
                    { label: '姓名', name: 'userName', index: 'user_name', width: 80 },
                    { label: '性别', name: 'sex', index: 'sex', width: 80 ,formatter: getDictInfo},
                    { label: '年龄', name: 'age', index: 'age', width: 80 },
                    { label: '实际交付金额', name: 'sjjfje', index: 'sjjfje', width: 80 },
                    { label: '用工单位id', name: 'settlementId', index: 'settlement_id', width: 80 ,hidden:true},
                    { label: '用工单位名称', name: 'settlementName', index: 'settlement_name', width: 80 },
                    { label: '生效日期', name: 'startDate', index: 'start_date', width: 80 },
                    { label: '失效日期', name: 'endDate', index: 'end_date', width: 80 },
                    { label: '批次', name: 'batchCode', index: 'batch_code', width: 40 },
                    { label: '录入日期', name: 'entrytime', index: 'entrytime', width: 80 },
                    { label: '录入人', name: 'inputperson', index: 'inputperson', width: 80 },
                    { label: '一审日期', name: 'firstinstance', index: 'firstinstance', width: 80},
                    { label: '一审人', name: 'firstinstaperson', index: 'firstinstaperson', width: 80 },
                    { label: '二审日期', name: 'secondinstance', index: 'secondinstance', width: 80 },
                    { label: '二审人', name: 'secondinstperson', index: 'secondinstperson', width: 80 },
                    { label: '三审日期', name: 'threeinstance', index: 'threeinstance', width: 80 },
                    { label: '三审人', name: 'threeinsteperson', index: 'threeinsteperson', width: 80 },
                    { label: '审核状态', name: 'instastatus', index: 'instastatus', width: 80,formatter:getinstastatus,unformat:ungetinstastatus},
                    { label: '是否生效', name: 'enabled', index: 'enabled', width: 80 ,formatter:getenabled,unformat:ungetenabled},
                    { label: '付款状态', name: 'paystatus', index: 'paystatus', width: 80 ,formatter:getpaystatus,unformat:ungetpaystatus},
                    { label: '替换人', name: 'replacePerson', index: 'replace_person', width: 80 },
                    { label: '回款说明', name: 'bz', index: 'bz', width: 80 }
                ],
                viewrecords: true,
                height: 350,
                width:$(window).width()*0.96,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:false,
                //字段太多自动加载横轴滚动条
                shrinkToFit:false,
                autoScroll: true,
                multiselect: true,
                pager: "#jqGridPagerDet",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){


                }
            });
		},
        queryDetlist: function () {
            var ydIds = getSelectedRow();
            if(ydIds == null){
                return ;
            }
            vm.reloadDet();
        },
        queryDet: function () {
            $("#jqGridDet").trigger("reloadGrid");
        },
        updateDet: function (event) {
            var ids=$("#jqGridDet").jqGrid("getGridParam","selarrrow");
            if(ids.length==0){
                alert("请选择一条记录");
                return ;
            }else if(ids.length>1){
                alert("只能选择一条记录");
                return ;
			}
            vm.getDetInfo(ids[0]);

        },
        getDetInfo: function(yddetId){
            $.get("../sbydgldet/info/"+yddetId, function(r){
                vm.sbydglDet = r.sbydglDet;
                vm.openDialogDetView('明细修改',$("#windowdet"),'1100px','350px',1);
            });
        },
        openDialogDetView:function(title,url,width,height,type){//打开修改明细菜单
            if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
                width='auto';
                height='auto';
            }else{//如果是PC端，根据用户设置的width和height显示。

            }
            layer.open({
                type: type,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: title,
                area: [width, height],
                maxmin: true, //开启最大化最小化按钮
                content: url,
                btn: ['确定','关闭'],
                btn1:function(index, layero){
                    vm.saveOrUpdateDet();
                   // layer.close(index);
                },
                btn2:function(index){
                    layer.close(index);
                }
            });
		},
        saveOrUpdateDet: function (event) {
            var url = vm.sbydglDet.yddetId == null ? "../sbydgldet/save" : "../sbydgldet/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.sbydglDet),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            $("#jqGridDet").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        delDet: function (event) {
            var ids=$("#jqGridDet").jqGrid("getGridParam","selarrrow");
            if(ids.length==0){
                alert("请选择一条记录");
                return ;
            }
            for(var i=0;i<ids.length;i++){
                var rowData=$("#jqGridDet").jqGrid("getRowData",ids[i]);
                if(rowData.instastatus!="0"&&rowData.instastatus!=null&&rowData.instastatus!=""){
                    alert('选中记录中有已审批的数据!')
                    return;
                }
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "../sbydgldet/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGridDet").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        reloadDet: function (event) {
            var page = $("#jqGridDet").jqGrid('getGridParam','page');
            $("#jqGridDet").jqGrid('setGridParam',{
                datatype:'json',
                postData : {
                	ydId :getSelectedRow(),
                    username: vm.qdet.username,
                    startDate: vm.qdet.startDate,
                    endDate: vm.qdet.endDate,
                    enabled:vm.qdet.enabled
				},
                page:page
            }).trigger("reloadGrid");
        utils.openDialogView('明细查看',$("#windowdetlist"),'1200px','550px',1)
        },
        firstaudit:function(){
            var ids=$("#jqGridDet").jqGrid("getGridParam","selarrrow");
            if(ids.length==0){
                alert("请选择一条记录");
                return ;
            }else if(ids.length>1){
                alert("只能选择一条记录");
                return ;
            }
            var rowData = $("#jqGridDet").jqGrid('getRowData',ids[0]);
            if(rowData.instastatus!='0'){
                alert("该条记录已在审批中或者还未到达一审节点！");
                return;
            }
            utils.openAuditView("../sbydgldet/simpleaudit",1,ids[0],$("#jqGridDet"));
        },
        secondaudit:function(){
            var ids=$("#jqGridDet").jqGrid("getGridParam","selarrrow");
            if(ids.length==0){
                alert("请选择一条记录");
                return ;
            }else if(ids.length>1){
                alert("只能选择一条记录");
                return ;
            }
            var rowData = $("#jqGridDet").jqGrid('getRowData',ids[0]);
            if(rowData.instastatus=='1'){
                utils.openAuditView("../sbydgldet/simpleaudit",2,ids[0],$("#jqGridDet"));
            }else{
                alert("该条记录已在审批中或者还未到二审节点！");
                return;
            }

        },
        thirdaudit:function(){
            var ids=$("#jqGridDet").jqGrid("getGridParam","selarrrow");
            if(ids.length==0){
                alert("请选择一条记录");
                return ;
            }else if(ids.length>1){
                alert("只能选择一条记录");
                return ;
            }
            var rowData = $("#jqGridDet").jqGrid('getRowData',ids[0]);
            if(rowData.instastatus=='2'){
                utils.openAuditView("../sbydgldet/simpleaudit",3,ids[0],$("#jqGridDet"));
            }else{
                alert("该条记录已在审批中或者还未到三审节点！");
                return;
            }

        },
        replaceperson:function(){
            var ids=$("#jqGridDet").jqGrid("getGridParam","selarrrow");
            if(ids.length==0){
                alert("请选择一条记录");
                return ;
            }else if(ids.length>1){
                alert("只能选择一条记录");
                return ;
            }
            var rowData = $("#jqGridDet").jqGrid('getRowData',ids[0]);
            if(rowData.enabled!='1'){
                alert("请选择一条已生效的记录！");
                return;
            }
            vm.reloadSelectDet();
        },
        getSelectDet:function(){
            $("#jqGridDetselect").jqGrid({
                url: '../sbydgldet/list',
                datatype: "local",
                colModel: [
                    { label: 'yddetId', name: 'yddetId', index: 'yddet_id', width: 50, key: true ,hidden:true},
                    { label: '身份证', name: 'idNo', index: 'id_no', width: 80 },
                    { label: '姓名', name: 'userName', index: 'user_name', width: 80 },
                    { label: '性别', name: 'sex', index: 'sex', width: 80 ,formatter: getDictInfo},
                    { label: '年龄', name: 'age', index: 'age', width: 80 },
                    { label: '实际交付金额', name: 'sjjfje', index: 'sjjfje', width: 80 },
                    { label: '用工单位名称', name: 'settlementName', index: 'settlement_name', width: 80 },
                    { label: '生效日期', name: 'startDate', index: 'start_date', width: 80 },
                    { label: '失效日期', name: 'endDate', index: 'end_date', width: 80 },
                    { label: '是否生效', name: 'enabled', index: 'enabled', width: 80 ,formatter:getenabled,unformat:ungetenabled},
                    { label: '替换人', name: 'replacePerson', index: 'replace_person', width: 80 },
                    { label: '回款说明', name: 'bz', index: 'bz', width: 80 }
                ],
                viewrecords: true,
                height: 200,
                width:$(window).width()*0.7,
                rowNum: 5,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:false,
                //字段太多自动加载横轴滚动条
                shrinkToFit:false,
                autoScroll: true,
                multiselect: false,
                pager: "#jqGridPagerDetselect",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){


                }
            });
        },
        reloadSelectDet: function (event) {
            var page = $("#jqGridDetselect").jqGrid('getGridParam','page');
            $("#jqGridDetselect").jqGrid('setGridParam',{
                datatype:'json',
                postData : {
                    ydId :getSelectedRow(),
                    enabled:'0',
                    batchcode:'2'
                },
                page:page
            }).trigger("reloadGrid");
            utils.openDialogView('明细替换',$("#selectwindowdetlist"),'900px','400px',1)
        },
        replace:function(){
            var replace_id=$("#jqGridDetselect").jqGrid("getGridParam","selrow");
                if(replace_id==null){
                    alert("请选择一条记录");
                    return ;
                }
            var replaced_id=$("#jqGridDet").jqGrid("getGridParam","selrow");
            var params={
                "replaced_id":replaced_id,
                "replace_id":replace_id
            };
            $.ajax({
                type: "POST",
                url: "../sbydgldet/replace",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGridDet").trigger("reloadGrid");
                            utils.closeCurrentPage();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getbatchcode:function(){
            $.ajax({
                type: "POST",
                url: "../sbydgl/getbatchcode",
                contentType: "application/json",
                // data: JSON.stringify(params),
                dataType:"json",
                async:false,
                cache:false,
                success: function(r){
                    if(r.code === 0){
                        vm.batchcodelist = r.batchcodelist;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        addBusinesspay:function () {
            var ids=$("#jqGridDet").jqGrid("getGridParam","selarrrow");
            if(ids.length==0){
                alert("请选择一条记录");
                return ;
            }
            vm.getbatchcode();
            utils.openDialogView('选择批次号',$("#windowbatchcode"),'450px','200px',1);
        },
        windowbatchcodesubmit:function(){
            var ids=$("#jqGridDet").jqGrid("getGridParam","selarrrow");
            if(ids.length==0){
                alert("请选择一条记录");
                return ;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',getSelectedRow());
            if(rowData.bxCode==""||rowData.bxCode==null){
                alert("此单保险编号未填写！");
                return ;
            }
            var batchcode = $('#selectbatchcode option:selected').val();
            $.ajax({
                type: "POST",
                url: "../sbydgl/addBusinesspay?batchcode="+batchcode+"&&ydid="+getSelectedRow(),
                contentType: "application/json",
                data: JSON.stringify(ids),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            utils.closeCurrentPage();
                            $("#jqGridDet").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        windowbatchcodeclose:function(){
            utils.closeCurrentPage();
        }
	}
});
function startDate(value){
    vm.qdet.startDate=value;
    //console.log(vm.dfsp.zfDate);
}
function endDate(value){
    vm.qdet.endDate=value;
    //console.log(vm.dfsp.zfDate);
}
function startDateDet(value){
    vm.sbydglDet.startDate=value;
    //console.log(vm.dfsp.zfDate);
}
function getinstastatus(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="0"){
        html="未审核";
    }else if(cellvalue=="1"){
        html="一级已审核";
    }else if(cellvalue=="2"){
        html="二级已审核";
    }else if(cellvalue=="3"){
        html="三级已审核";
    }
    return html;
}
function ungetinstastatus(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="未审核"){
        html="0";
    }else if(cellvalue=="一级已审核"){
        html="1";
    }else if(cellvalue=="二级已审核"){
        html="2";
    }else if(cellvalue=="三级已审核"){
        html="3";
    }
    return html;
}
function getenabled(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="0"){
        html="未生效";
    }else if(cellvalue=="1"){
        html="已生效";
    }else if(cellvalue=="2"){
        html="已替换";
    }
    return html;
}
function ungetenabled(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="未生效"){
        html="0";
    }else if(cellvalue=="已生效"){
        html="1";
    }else if(cellvalue=="已替换"){
        html="2";
    }
    return html;
}
function getpaystatus(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="0"){
        html="未付款";
    }else if(cellvalue=="1"){
        html="付款中";
    }else if(cellvalue=="2"){
        html="已付款";
    }
    return html;
}
function ungetpaystatus(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="未付款"){
        html="0";
    }else if(cellvalue=="付款中"){
        html="1";
    }else if(cellvalue=="已付款"){
        html="2";
    }
    return html;
}
